import cProfile
from euler.numbers.decimal_base import join_integers


def get_valid_maximal_strings_for_gon_ring(number_of_inner_elements):
    number_of_elements = number_of_inner_elements * 2
    first_number_of_maximal_string = number_of_elements - number_of_inner_elements + 1
    possible_inner_values = [i for i in xrange(1, number_of_elements + 1) if i != first_number_of_maximal_string]

    def inner_get_valid_strings(inner_elements):
        if len(inner_elements) < number_of_inner_elements:
            for i in possible_inner_values:
                if i not in inner_elements:
                    for string in inner_get_valid_strings(inner_elements + (i,)):
                        yield string
        else:
            bin = [0 if i + 1 != first_number_of_maximal_string else 1 for i in xrange(number_of_elements)]

            elements = [first_number_of_maximal_string, inner_elements[0], inner_elements[1]]
            target_number = sum(elements)

            bin[inner_elements[0] - 1] = 1

            for i in xrange(1, len(inner_elements)):
                bin[inner_elements[i] - 1] = 1

                first_inner_number = inner_elements[i]
                second_inner_number = inner_elements[(i + 1) % len(inner_elements)]

                outer_number = target_number - first_inner_number - second_inner_number

                elements.append(outer_number)
                elements.append(first_inner_number)
                elements.append(second_inner_number)

                if outer_number <= first_number_of_maximal_string or outer_number > number_of_elements:
                    return

                bin[outer_number - 1] = 1

            if all(i == 1 for i in bin):
                yield elements

    return inner_get_valid_strings(tuple())

def get_answer():
    max_number = 10**16

    candidates = (join_integers(elements) for elements in get_valid_maximal_strings_for_gon_ring(5))
    return max(candidate for candidate in candidates if candidate < max_number)


if __name__ == "__main__":
    cProfile.run("print(get_answer())")
